/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.dxjy.demo.module.sys.mapper;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dxjy.demo.config.datasources.DataSourceNames;
import com.dxjy.demo.config.datasources.annotation.DataSource;
import com.dxjy.demo.module.sys.entity.SysDep;
import com.dxjy.demo.module.sys.entity.SysRole;
import com.dxjy.demo.module.sys.entity.SysUser;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 查询所有记录
     * 使用mybatis xml
     * @return
     */
    List<SysUser> getUserList();


    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT u.id, u.username, u.realName, u.email, u.phone, u.create_date, u.lastLoginDate, u.lastLoginIp, " +
            "u.`status`, r.`name` roleName, d.`name` depName\n" +
            "    FROM sys_user u\n" +
            "    INNER JOIN sys_role r\n" +
            "    INNER JOIN sys_department d\n" +
            "    ON u.roleId = r.id\n" +
            "    AND u.dep_id = d.id")
    List<JSONObject> getUserInfoList();


    /**
     * 查询部门列表
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT id, pid, `name`, create_date, `status`, sort FROM sys_department")
    List<SysDep> getDepList();

    /**
     * 根据用户查询数据
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT * FROM sys_user WHERE username = #{username}")
    SysUser getUserByName(String username);


    /**
     * 根据用户id查询数据
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT * FROM sys_user WHERE id = #{id}")
    SysUser getUserByUserId(long id);

    /**
     * 获得密码
     * @param id
     * @return
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT password FROM sys_user WHERE id = #{id}")
    String getPassword(long id);


    /**
     * 检查用户状态
     * 0:新注册未验证  1：验证不通过  2：验证通过 3：停用
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT status FROM sys_user WHERE id = #{id}")
    int checkUserStatus(long id);

    /**
     * 获得角色
     * @param id
     * @return
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Select("SELECT r.id, r.`name`, r.perms, r.note FROM sys_role r INNER JOIN sys_user u " +
            "ON r.id = u.roleId " +
            "AND u.id = #{id}")
    SysRole getRole(long id);


    /**
     * 更新状态
     * @param status
     * @param id
     * @return
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Update("UPDATE sys_user set status = #{status} WHERE id = #{id}")
    boolean updateUserStatus(int status, long id);


    /**
     * 修改密码
     * @param password
     * @param id
     * @return
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Update("UPDATE sys_user set password = #{password} WHERE id = #{id}")
    boolean updateUserPassword(String password, long id);


    /**
     * 新增用户
     * @param user
     * @return
     */
    @DataSource(name = DataSourceNames.FIRST)
    @Insert("INSERT INTO `sys_user` (`username`, `password`, `realName`, `email`, `phone`, `roleId`, " +
            "`lastLoginDate`, `lastLoginIp`, `status`, `registDate`) " +
            "VALUES (#{username}, #{password}, #{realName}, #{email}, #{phone}, #{roleId}, " +
            "#{lastLoginDate}, #{lastLoginIp}, #{status}, #{registDate})")
    boolean insertUser(SysUser user);


//    /**
//     * 获得用户角色默认的权限
//     */
//    @DataSource(name = DataSourceNames.FIRST)
//    @Select("SELECT role.permission FROM sys_user, role WHERE username = #{username} AND role.role = user.role")
//    String getRolePermission(String username);
//
//    /**
//     * 获得用户的权限
//     */
//    @DataSource(name = DataSourceNames.FIRST)
//    @Select("SELECT permission FROM sys_user WHERE username = #{username}")
//    String getPermission(String username);
}
